package com.cy.aircompressor.main.ui.systemsetting.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.Observer;

import com.cy.aircompressor.api.RepositoryImpl;
import com.cy.aircompressor.entity.power.PowerStation;
import com.cy.aircompressor.entity.sensor.Sensor;
import com.cy.aircompressor.entity.system.UserInfo;
import com.drug.lib.util.DLog;
import com.drug.lib.view.recycptr.bean.RowEntity;
import com.net.model.base.Resource;
import com.net.model.viewmodel.BaseViewModel;

import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;

/**
 * 版权：周和权 版权所有
 *
 * @author zhouhequan
 * 版本：1.0
 * 创建日期：2021/11/12 or 23:27
 * 描述：---
 */
public class SystemSettingViewModel extends BaseViewModel<RepositoryImpl> {
    private MediatorLiveData<Resource<RowEntity<UserInfo>>> queryUserInfoByPage;
    private MediatorLiveData<Resource<UserInfo>> queryUserInfoById;
    private MediatorLiveData<Resource<Object>> addUser;
    private MediatorLiveData<Resource<Object>> updateUser;
    private MediatorLiveData<Resource<Object>> deleteByIds;


    private MediatorLiveData<Resource<RowEntity<PowerStation>>> queryPowerStationByPage;
    private MediatorLiveData<Resource<PowerStation>> queryPowerStationById;
    private MediatorLiveData<Resource<Object>> addPowerStation;
    private MediatorLiveData<Resource<Object>> updatePowerStation;
    private MediatorLiveData<Resource<Object>> deletePowerStationByIds;

    public SystemSettingViewModel(@NonNull Application application) {
        super(application);
        bindBaseModel(RepositoryImpl.get());
        queryUserInfoByPage = new MediatorLiveData<>();
        queryUserInfoById = new MediatorLiveData<>();
        addUser = new MediatorLiveData<>();
        updateUser = new MediatorLiveData<>();
        deleteByIds = new MediatorLiveData<>();
        queryPowerStationByPage = new MediatorLiveData<>();
        queryPowerStationById = new MediatorLiveData<>();
        addPowerStation = new MediatorLiveData<>();
        updatePowerStation = new MediatorLiveData<>();
        deletePowerStationByIds = new MediatorLiveData<>();
    }


    public void queryUserInfoByPage(Integer pageNum, Integer pageSize, String account, String name, String powerStationId) {
        queryUserInfoByPage.addSource(baseModel.queryUserInfoByPage(pageNum, pageSize, account, name, powerStationId), new Observer<Resource<RowEntity<UserInfo>>>() {
            @Override
            public void onChanged(Resource<RowEntity<UserInfo>> rowEntityResource) {
                queryUserInfoByPage.setValue(rowEntityResource);
            }
        });
    }

    public void queryUserInfoById(String id) {
        queryUserInfoById.addSource(baseModel.queryUserInfoById(id), new Observer<Resource<UserInfo>>() {
            @Override
            public void onChanged(Resource<UserInfo> rowEntityResource) {
                queryUserInfoById.setValue(rowEntityResource);
            }
        });
    }

    public void addUser(Object parameters) {
        addUser.addSource(baseModel.addUser(parameters), new Observer<Resource<Object>>() {
            @Override
            public void onChanged(Resource<Object> rowEntityResource) {
                addUser.setValue(rowEntityResource);
            }
        });
    }

    public void updateUser(Object parameters) {
        updateUser.addSource(baseModel.updateUser(parameters), new Observer<Resource<Object>>() {
            @Override
            public void onChanged(Resource<Object> rowEntityResource) {
                updateUser.setValue(rowEntityResource);
            }
        });
    }

    public void deleteByIds(String[] ids) {
        deleteByIds.addSource(baseModel.deleteByIds(ids), new Observer<Resource<Object>>() {
            @Override
            public void onChanged(Resource<Object> rowEntityResource) {
                deleteByIds.setValue(rowEntityResource);
            }
        });
    }


    public void queryPowerStationByPage(Integer pageNum, Integer pageSize, String name) {
        queryPowerStationByPage.addSource(baseModel.queryPowerStationByPage(pageNum, pageSize, name), new Observer<Resource<RowEntity<PowerStation>>>() {
            @Override
            public void onChanged(Resource<RowEntity<PowerStation>> rowEntityResource) {
                queryPowerStationByPage.setValue(rowEntityResource);
            }
        });
    }

    public void queryPowerStationById(String id) {
        queryPowerStationById.addSource(baseModel.queryPowerStationById(id), new Observer<Resource<PowerStation>>() {
            @Override
            public void onChanged(Resource<PowerStation> rowEntityResource) {
                queryPowerStationById.setValue(rowEntityResource);
            }
        });
    }

    public void addPowerStation(Object parameters) {
        addPowerStation.addSource(baseModel.addPowerStation(parameters), new Observer<Resource<Object>>() {
            @Override
            public void onChanged(Resource<Object> rowEntityResource) {
                addPowerStation.setValue(rowEntityResource);
            }
        });
    }

    public void updatePowerStation(Object parameters) {
        updatePowerStation.addSource(baseModel.updatePowerStation(parameters), new Observer<Resource<Object>>() {
            @Override
            public void onChanged(Resource<Object> rowEntityResource) {
                updatePowerStation.setValue(rowEntityResource);
            }
        });
    }

    public void deletePowerStationByIds(String[] ids) {
        deletePowerStationByIds.addSource(baseModel.deletePowerStationByIds(ids), new Observer<Resource<Object>>() {
            @Override
            public void onChanged(Resource<Object> rowEntityResource) {
                deletePowerStationByIds.setValue(rowEntityResource);
            }
        });
    }


    public MediatorLiveData<Resource<RowEntity<UserInfo>>> getQueryUserInfoByPage() {
        return queryUserInfoByPage;
    }

    public MediatorLiveData<Resource<UserInfo>> getQueryUserInfoById() {
        return queryUserInfoById;
    }

    public MediatorLiveData<Resource<Object>> getAddUser() {
        return addUser;
    }

    public MediatorLiveData<Resource<Object>> getUpdateUser() {
        return updateUser;
    }

    public MediatorLiveData<Resource<Object>> getDeleteByIds() {
        return deleteByIds;
    }

    public MediatorLiveData<Resource<RowEntity<PowerStation>>> getQueryPowerStationByPage() {
        return queryPowerStationByPage;
    }

    public MediatorLiveData<Resource<PowerStation>> getQueryPowerStationById() {
        return queryPowerStationById;
    }

    public MediatorLiveData<Resource<Object>> getAddPowerStation() {
        return addPowerStation;
    }

    public MediatorLiveData<Resource<Object>> getUpdatePowerStation() {
        return updatePowerStation;
    }

    public MediatorLiveData<Resource<Object>> getDeletePowerStationByIds() {
        return deletePowerStationByIds;
    }
}
